package com.haiyangwang1.frames;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    String user = "admin";
    String pwd = "12345";
    private JTextField txt;
    private JPasswordField pwd1;
    int count;//标记点击按钮的次数

    public LoginFrame() {
        this.txt = txt;
        this.pwd1 = pwd1;
    }

    public JTextField getTxtName() {
        return txt;
    }

    public void setTxtName(JTextField txtName) {
        this.txt = txt;
    }

    public JPasswordField getTxtPassword() {
        return pwd1;
    }

    public void setTxtPassword(JPasswordField txtPassword) {
        this.pwd1 = pwd1;
    }

    public static void main(String[] args) {
        LoginFrame loginFrame = new LoginFrame();
        loginFrame.loginFrame();
    }

    public void loginFrame() {
        JFrame login = new JFrame();
        login.setTitle("登录窗口");
        login.setLocation(300, 300);
        login.setSize(500, 300);
        login.setLayout(new GridLayout(3, 2));

        JPanel p1 = new JPanel();
        JLabel lbl = new JLabel("账号：");
        JTextField txt = new JTextField(20);
        p1.add(lbl);
        p1.add(txt);

        JPanel p2 = new JPanel();
        JLabel lbl1 = new JLabel("密码：");
        JPasswordField pwd = new JPasswordField(20);
        p2.add(lbl1);
        p2.add(pwd);

        JPanel p3 = new JPanel();//默认flowlayout  流式布局
        JButton btn = new JButton("登录");
        JButton btn1 = new JButton("取消");
        p3.add(btn);
        p3.add(btn1);
//        login.add(btn);

        login.add(p1);
        login.add(p2);
        login.add(p3);

        //默认的是borderlayout

        MyActionListener myActionListener = new MyActionListener(txt,pwd1);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = txt.getText();
                String pwd1 = new String(pwd.getPassword());

                if (e.getSource()==btn){//监听登录按键

                if (username.equals("admin")&&pwd1.equals("12345")) {
                    JOptionPane.showMessageDialog(null, "恭喜你，登录成功！");
                    CharFrame charFrame = new CharFrame();

                    login.dispose();
                }else {
                    if (count>=3){
                        System.out.println("输出次数过多，退出登录页面");
                        System.exit(0);
                    }else {
                        //显示信息框
                    JOptionPane.showMessageDialog(null,"用户名或密码错误，请重新登录,你还有"+(3-count)+"次机会");
                    //确认信息框  会有一个int的返回值，可以通过这个返回值判断后面需要做什么事情
//                    int n =JOptionPane.showConfirmDialog(null,"");
                    //输入确认框  当需要获取一个简单的用户输入的信息时可以使用
//                    String str = JOptionPane.showInputDialog(null,"请输入你的名字：");
                    count=count+1;
                    txt.setText("");
                    pwd.setText("");
                    }
                }
            }
            }
        });
//        login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        login.setVisible(true);

    }
}
